using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;

public class MainMenuManager : MonoBehaviour
{
    [Header("菜单面板")]
    [SerializeField] private GameObject mainPanel;
    [SerializeField] private GameObject levelSelectPanel;
    [SerializeField] private GameObject optionsPanel;
    [SerializeField] private GameObject creditsPanel;
    
    [Header("关卡选择")]
    [SerializeField] private Transform levelButtonContainer;
    [SerializeField] private GameObject levelButtonPrefab;
    [SerializeField] private List<LevelData> availableLevels = new List<LevelData>();
    
    [Header("音频")]
    [SerializeField] private AudioSource musicSource;
    [SerializeField] private AudioSource sfxSource;
    [SerializeField] private AudioClip buttonClickSound;
    [SerializeField] private AudioClip backgroundMusic;
    
    [Header("设置")]
    [SerializeField] private Slider musicVolumeSlider;
    [SerializeField] private Slider sfxVolumeSlider;
    
    // 关卡数据结构
    [System.Serializable]
    public class LevelData
    {
        public string levelName;
        public string sceneName;
        public Sprite levelImage;
        public bool isLocked = false;
        public int requiredScore = 0; // 解锁所需分数
    }
    
    private void Start()
    {
        // 初始化菜单
        ShowMainPanel();
        
        // 播放背景音乐
        if (musicSource != null && backgroundMusic != null)
        {
            musicSource.clip = backgroundMusic;
            musicSource.Play();
        }
        
        // 初始化音量滑块
        if (musicVolumeSlider != null)
        {
            musicVolumeSlider.value = PlayerPrefs.GetFloat("MusicVolume", 0.75f);
            musicVolumeSlider.onValueChanged.AddListener(SetMusicVolume);
        }
        
        if (sfxVolumeSlider != null)
        {
            sfxVolumeSlider.value = PlayerPrefs.GetFloat("SFXVolume", 0.75f);
            sfxVolumeSlider.onValueChanged.AddListener(SetSFXVolume);
        }
        
        // 应用音量设置
        SetMusicVolume(musicVolumeSlider != null ? musicVolumeSlider.value : 0.75f);
        SetSFXVolume(sfxVolumeSlider != null ? sfxVolumeSlider.value : 0.75f);
        
        // 生成关卡按钮
        GenerateLevelButtons();
    }
    
    // 生成关卡选择按钮
    private void GenerateLevelButtons()
    {
        if (levelButtonContainer == null || levelButtonPrefab == null) return;
        
        // 清除现有按钮
        foreach (Transform child in levelButtonContainer)
        {
            Destroy(child.gameObject);
        }
        
        // 生成新按钮
        for (int i = 0; i < availableLevels.Count; i++)
        {
            LevelData level = availableLevels[i];
            GameObject buttonObj = Instantiate(levelButtonPrefab, levelButtonContainer);
            
            // 设置按钮文本和图像
            Text buttonText = buttonObj.GetComponentInChildren<Text>();
            if (buttonText != null)
            {
                buttonText.text = level.levelName;
            }
            
            Image buttonImage = buttonObj.GetComponent<Image>();
            if (buttonImage != null && level.levelImage != null)
            {
                buttonImage.sprite = level.levelImage;
            }
            
            // 检查关卡是否解锁
            bool isUnlocked = !level.isLocked || PlayerPrefs.GetInt("HighScore_" + level.sceneName, 0) >= level.requiredScore;
            
            // 设置按钮交互性
            Button button = buttonObj.GetComponent<Button>();
            if (button != null)
            {
                button.interactable = isUnlocked;
                
                // 添加点击事件
                int levelIndex = i; // 捕获当前索引
                button.onClick.AddListener(() => {
                    PlayButtonSound();
                    LoadLevel(levelIndex);
                });
            }
            
            // 显示锁定状态
            GameObject lockIcon = buttonObj.transform.Find("LockIcon")?.gameObject;
            if (lockIcon != null)
            {
                lockIcon.SetActive(!isUnlocked);
            }
        }
    }
    
    // 显示主面板
    public void ShowMainPanel()
    {
        HideAllPanels();
        if (mainPanel != null) mainPanel.SetActive(true);
    }
    
    // 显示关卡选择面板
    public void ShowLevelSelectPanel()
    {
        PlayButtonSound();
        HideAllPanels();
        if (levelSelectPanel != null) levelSelectPanel.SetActive(true);
    }
    
    // 显示选项面板
    public void ShowOptionsPanel()
    {
        PlayButtonSound();
        HideAllPanels();
        if (optionsPanel != null) optionsPanel.SetActive(true);
    }
    
    // 显示制作人员面板
    public void ShowCreditsPanel()
    {
        PlayButtonSound();
        HideAllPanels();
        if (creditsPanel != null) creditsPanel.SetActive(true);
    }
    
    // 隐藏所有面板
    private void HideAllPanels()
    {
        if (mainPanel != null) mainPanel.SetActive(false);
        if (levelSelectPanel != null) levelSelectPanel.SetActive(false);
        if (optionsPanel != null) optionsPanel.SetActive(false);
        if (creditsPanel != null) creditsPanel.SetActive(false);
    }
    
    // 加载关卡
    public void LoadLevel(int levelIndex)
    {
        if (levelIndex >= 0 && levelIndex < availableLevels.Count)
        {
            string sceneName = availableLevels[levelIndex].sceneName;
            if (!string.IsNullOrEmpty(sceneName))
            {
                SceneManager.LoadScene(sceneName);
            }
        }
    }
    
    // 设置音乐音量
    public void SetMusicVolume(float volume)
    {
        if (musicSource != null)
        {
            musicSource.volume = volume;
        }
        PlayerPrefs.SetFloat("MusicVolume", volume);
        PlayerPrefs.Save();
    }
    
    // 设置音效音量
    public void SetSFXVolume(float volume)
    {
        if (sfxSource != null)
        {
            sfxSource.volume = volume;
        }
        PlayerPrefs.SetFloat("SFXVolume", volume);
        PlayerPrefs.Save();
    }
    
    // 播放按钮音效
    private void PlayButtonSound()
    {
        if (sfxSource != null && buttonClickSound != null)
        {
            sfxSource.PlayOneShot(buttonClickSound);
        }
    }
    
    // 退出游戏
    public void QuitGame()
    {
        PlayButtonSound();
        
        #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
        #else
        Application.Quit();
        #endif
    }
}